﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Erebos.GameScreenFiles.WorldObjects
{
    class FireObject : Object
    {
        private ParticleSystem _fireParticles;
        private ParticleSystem _smokePlumeParticles;

        private Vector3 _position;
        private Vector3 _size;

        private int _intensivität;

        private Random _random = new Random();

        public FireObject(Vector3 position, Vector3 size, int intensivität)
        {
            _position = position;
            _size = size;
            _intensivität = intensivität;

            _fireParticles = new FireParticleSystem(MainGame.Game, MainGame.GetContentManager());
            _fireParticles.Settings.MaxParticles = intensivität;
            _smokePlumeParticles = new SmokePlumeParticleSystem(MainGame.Game, MainGame.GetContentManager(), _fireParticles.Settings.Gravity);

            MainGame.Game.Components.Add(_fireParticles);
            MainGame.Game.Components.Add(_smokePlumeParticles);
        }

        public override void Draw(GameTime gameTime)
        {
            _fireParticles.SetCamera(Camera.ViewMatrix, Camera.ProjectionMatrix);
            _smokePlumeParticles.SetCamera(Camera.ViewMatrix, Camera.ProjectionMatrix);
            base.Draw(gameTime);
        }

        public override void Update(GameTime gameTime)
        {
            _fireParticles.DrawOrder = 2024 * 2024 - (int)Vector2.Distance(new Vector2(Camera.CameraPosition.X, -Camera.CameraPosition.Z), new Vector2(_position.X, _position.Z));
            _smokePlumeParticles.DrawOrder = 2024 * 2024 - (int)Vector2.Distance(new Vector2(Camera.CameraPosition.X, -Camera.CameraPosition.Z), new Vector2(_position.X, _position.Z));

            for (int i = 0; i < _intensivität / 100; i++)
            {
                _fireParticles.AddParticle(new Vector3(_position.X + _random.Next(0, (int)_size.X), _position.Y + _random.Next(0, (int)_size.Y), -_position.Z - _random.Next(0, (int)_size.Z)), Vector3.Zero);
            }

            if(_random.Next(0, 3) == 0)
                _smokePlumeParticles.AddParticle(new Vector3(_position.X + _random.Next(0, (int)_size.X), _position.Y + _random.Next(0, (int)_size.Y), -_position.Z - _random.Next(0, (int)_size.Z)), Vector3.Zero);

            base.Update(gameTime);
        }
    }
}
